﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace base64
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Multiselect = true;//该值确定是否可以选择多个文件
                dialog.Title = "请选择文件";
                dialog.Filter = "txt格式（*.txt）|*.txt|所有文件|*.*";


                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    richTextBox1.Clear();
                    string file = dialog.FileName;
                    /*richTextBox1.AppendText(file + "\n");*///显示路径，并且后面的叠加不删除
                    StreamReader sr = new StreamReader(file, Encoding.Default);
                    String line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(richTextBox1.Text += line + "\r\n");
                        System.Text.Encoding encode = System.Text.Encoding.ASCII;
                        byte[] outputb = Convert.FromBase64String(line);
                        string orgStr = Encoding.Default.GetString(outputb);//更改输出语言
                        //Console.OutputEncoding = System.Text.Encoding.Unicode;
                        Console.WriteLine(richTextBox2.Text += orgStr + "\r\n");
                    }
                }
            }
            catch(Exception ex)
            {
                Console.WriteLine(richTextBox1.Text = "" + ex);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Text == string.Empty)
            {
                MessageBox.Show("编码数据不能为空！");
                return;
            }
            //单行解码
            richTextBox2.Clear();
            string base4s = richTextBox1.Text;
            try
            {
                byte[] outputb = Convert.FromBase64String(base4s);
                string orgStr = Encoding.Default.GetString(outputb);
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                Console.WriteLine(richTextBox2.Text += orgStr + "\r\n");
            }
            catch(Exception ex)
            {
                Console.WriteLine(richTextBox2.Text = "" + ex);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if(richTextBox1.Text == string.Empty)
            {
                MessageBox.Show("编码数据不能为空！");
                return;
            }
            richTextBox2.Clear();//做清空操作
            string base4s = richTextBox1.Text;
            try
            {
                byte[] bytes = Encoding.Default.GetBytes(base4s);//获取需要编码的字符串
                string data = Convert.ToBase64String(bytes);//通过默认编码
                Console.WriteLine(richTextBox2.Text += data + "\r\n");
                //for(int i = 0;i < data.Length; i++)
                //{
                //    Console.WriteLine(richTextBox2.Text += data + "\r\n");
                //}
                string shiliu = richTextBox2.Text;
                byte[] bys = Convert.FromBase64String(shiliu);
                string hex = BitConverter.ToString(bys);
                Console.WriteLine(richTextBox3.Text = hex + "\r\n");
            }
            catch(Exception ex)
            {
                Console.WriteLine(richTextBox2.Text = "" + ex);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Multiselect = true;//该值确定是否可以选择多个文件
                dialog.Title = "请选择文件";
                dialog.Filter = "txt格式（*.txt）|*.txt|所有文件|*.*";


                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    richTextBox1.Clear();
                    string file = dialog.FileName;
                    /*richTextBox1.AppendText(file + "\n");*///显示路径，并且后面的叠加不删除
                    StreamReader sr = new StreamReader(file, Encoding.Default);
                    String line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(richTextBox1.Text += line + "\r\n");
                        System.Text.Encoding encode = System.Text.Encoding.ASCII;
                        byte[] bytes = Encoding.Default.GetBytes(line);//获取循环读取的字符串
                        string data = Convert.ToBase64String(bytes);//通过默认编码
                        //Console.OutputEncoding = System.Text.Encoding.Unicode;
                        Console.WriteLine(richTextBox2.Text += data + "\r\n");
                        string shiliu = richTextBox2.Text;
                        byte[] bys = Convert.FromBase64String(shiliu);
                        string hex = BitConverter.ToString(bys);
                        Console.WriteLine(richTextBox3.Text = hex + "\r\n");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(richTextBox1.Text = "" + ex);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //单行二次交换解码
            if(richTextBox2.Text == string.Empty)
            {
                MessageBox.Show("交换解码失败！请查看是否进行解码！");
                return;
            }
            richTextBox2.Clear();
            string base4s = richTextBox1.Text;
            try
            {
                byte[] outputb = Convert.FromBase64String(base4s);
                string orgStr = Encoding.Default.GetString(outputb);
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                Console.WriteLine(richTextBox2.Text += orgStr + "\r\n");
            }
            catch (Exception ex)
            {
                Console.WriteLine(richTextBox2.Text = "" + ex);
            }
        }
        //Thread th;
        private void button6_Click(object sender, EventArgs e)
        {
            if(richTextBox2.Text == string.Empty)
            {
                MessageBox.Show("请先进行编码！");
                return;
            }
           richTextBox1.Clear();//先清空一在进行输出，做清空操作
            string base4s = richTextBox2.Text;
            try
            {
                byte[] bytes = Encoding.Default.GetBytes(base4s);//获取需要编码的字符串
                string data = Convert.ToBase64String(bytes);//通过默认编码
                Console.WriteLine(richTextBox1.Text += data + "\r\n");
                //button6.Text = "再次交换";
                //if(button6.Text == "再次交换")
                //{
                //    //创建一个线程
                //    th = new Thread(base6s1);
                //    th.IsBackground = true;
                //    th.Start();
                //}
                string shiliu = richTextBox2.Text;
                byte[] bys = Convert.FromBase64String(shiliu);
                string hex = BitConverter.ToString(bys);
                Console.WriteLine(richTextBox3.Text = hex + "\r\n");
            }
            catch (Exception ex)
            {
                Console.WriteLine(richTextBox1.Text = "" + ex);
                return;
            }
        }
        //第一次交换执行
        private void base6s1()
        {
            if(richTextBox2.Text == string.Empty)
            {
                MessageBox.Show("请先进行编码！");
                return;
            }
            string base6s11 = richTextBox1.Text;
            richTextBox2.Clear();
            try
            {
                byte[] bytes = Encoding.Default.GetBytes(base6s11);//获取需要编码的字符串
                string data = Convert.ToBase64String(bytes);//通过默认编码
                Console.WriteLine(richTextBox2.Text += data + "\r\n");
                //button6.Text = "再次交换";
                //if (button6.Text == "再次交换")
                //{
                //    //创建一个线程
                //    th = new Thread(base6s1);
                //    th.IsBackground = true;
                //    th.Start();
                //}
            }
            catch (Exception ex)
            {
                Console.WriteLine(richTextBox2.Text = "" + ex);
                return;
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            //多行二次交换解码
            try
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Multiselect = true;//该值确定是否可以选择多个文件
                dialog.Title = "请选择文件";
                dialog.Filter = "txt格式（*.txt）|*.txt|所有文件|*.*";


                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    richTextBox1.Clear();
                    string file = dialog.FileName;
                    /*richTextBox1.AppendText(file + "\n");*///显示路径，并且后面的叠加不删除
                    StreamReader sr = new StreamReader(file, Encoding.Default);
                    String line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(richTextBox1.Text += line + "\r\n");
                        System.Text.Encoding encode = System.Text.Encoding.ASCII;
                        byte[] outputb = Convert.FromBase64String(line);
                        string orgStr = Encoding.Default.GetString(outputb);
                        Console.WriteLine(richTextBox2.Text += orgStr + "\r\n");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(richTextBox1.Text = "" + ex);
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Multiselect = true;//该值确定是否可以选择多个文件
                dialog.Title = "请选择文件";
                dialog.Filter = "txt格式（*.txt）|*.txt|所有文件|*.*";


                if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    richTextBox1.Clear();
                    string file = dialog.FileName;
                    /*richTextBox1.AppendText(file + "\n");*///显示路径，并且后面的叠加不删除
                    StreamReader sr = new StreamReader(file, Encoding.Default);
                    String line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(richTextBox1.Text += line + "\r\n");
                        System.Text.Encoding encode = System.Text.Encoding.ASCII;
                        byte[] bytes = Encoding.Default.GetBytes(line);//获取需要编码的字符串
                        string data = Convert.ToBase64String(bytes);//通过默认编码
                        Console.WriteLine(richTextBox1.Text += data + "\r\n");

                        string shiliu = richTextBox2.Text;
                        byte[] bys = Convert.FromBase64String(shiliu);
                        string hex = BitConverter.ToString(bys);
                        Console.WriteLine(richTextBox3.Text = hex + "\r\n");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(richTextBox1.Text = "" + ex);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if(richTextBox1.Text == string.Empty)
            {
                MessageBox.Show("字符串不能为空！");
                return;
            }
            try
            {
                //去掉空格
                string str = richTextBox1.Text;
                //将字符串转换成字节数组。
                byte[] buffer = Encoding.Default.GetBytes(str);
                //定义一个string类型的变量，用于存储转换后的值。
                for (int i = 0; i < str.Length; i++)
                {
                    //将每一个字节数组转换成16进制的字符串，以空格相隔开。
                    Console.WriteLine(richTextBox3.Text = Convert.ToString(buffer[i], 16));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(richTextBox3.Text = "" + ex);
            }
        }
    }
}
